package frame.common;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 消息类，定义了一个通用的消息形式。其中包含消息主体和消息类型(成功，错误，提示等);
 * @author MR.CUIPENG
 *
 */
public class Message {
	
	//操作成功
	public static Message OK=new Message("000000",ConstantEntity.MESSAGE_TYPE_OK);
	//操作失败
	public static Message ERR=new Message("000001",ConstantEntity.MESSAGE_TYPE_OK);
	//新增成功
	public static Message ADD_OK=new Message("000002",ConstantEntity.MESSAGE_TYPE_OK);
	//编辑成功
	public static Message EDIT_OK=new Message("000003",ConstantEntity.MESSAGE_TYPE_OK);
	//删除成功
	public static Message DEL_OK=new Message("000004",ConstantEntity.MESSAGE_TYPE_OK);
	//新增失败
	public static Message ADD_ERR=new Message("000005",ConstantEntity.MESSAGE_TYPE_ERROR);
	//编辑失败
	public static Message EDIT_ERR=new Message("000006",ConstantEntity.MESSAGE_TYPE_ERROR);
	//删除失败
	public static Message DEL_ERR=new Message("000007",ConstantEntity.MESSAGE_TYPE_ERROR);
	//消息内容
	private String msg;
	//消息代码
	private String msgType;
	//替换者，按顺序替换消息中的占位符
	private List<Object> replacer=new ArrayList<Object>();

	public Message() {/*default constructor*/}
	
	public Message(String msg, String msgType) {
		this.msg = msg;
		this.msgType = msgType;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public void setMsgType(String msgType) {
		this.msgType = msgType;
	}
	
	public String getMsg() {
		return msg;
	}
	public String getMsgType() {
		return msgType;
	}
	
	public List<Object> getReplacer() {
		return replacer;
	}
	public void setReplacer(String replacer) {
		this.replacer.add(replacer);
	}
	public void setReplacer(Object[] replacer) {
		List<Object> rp=Arrays.asList(replacer);
		int len=rp.size();
		if(len>1){
			this.replacer.addAll(rp.subList(1,len));
		}
	}
	@Override
	public String toString() {
		return "{\"msg\":\"" + msg + "\", \"msgType\":\"" + msgType +  "\", \"replacer\":\""+replacer+"\"}";
	}
}
